package com.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pojo.Order;
import com.service.OrdersService;
import com.util.ConstUtils;
import com.vo.DataVO;
import com.vo.OrdersVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @author mornd
 * @date 2020/12/4 - 10:12
 */
@RestController
@RequestMapping("/orders")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;

    /**
     * 按条件查询订单集合
     *
     * @param ordersVo
     * @return
     */
    @RequestMapping("/list")
    public String findOrderList(OrdersVo ordersVo) {
        PageHelper.startPage(ordersVo.getPage(), ordersVo.getLimit());
        PageInfo<Order> pageInfo = new PageInfo<Order>(ordersService.findOrderList(ordersVo));
        return JSON.toJSONString(new DataVO<Order>(pageInfo.getTotal(), pageInfo.getList()));
    }

    /**
     * 修改订单
     *
     * @param order
     * @return
     */
    @RequestMapping("/confirmOrders")
    public Map<String, Object> updateOrder(Order order) {
        List<Integer> ids = new ArrayList<Integer>();
        ids.add(order.getId());
        Map<String, Object> maps = new HashMap<String, Object>();
        if (ordersService.confirmOrders(ids)) {
            maps.put(ConstUtils.SUCCESS, true);
            maps.put(ConstUtils.MESSAGE, "订单确认成功！");
        } else {
            maps.put(ConstUtils.SUCCESS, false);
            maps.put(ConstUtils.MESSAGE, "订单确认失败，请重试！");
        }
        return maps;
    }

    /**
     * 批量确认
     *
     * @return
     */
    @RequestMapping("/batchConfirm")
    public Map<String, Object> batchConfirm(String ids) {
        //将ids截取成数组
        String[] idsArr = ids.split(",");
        //转为Integer的集合
        List<Integer> list = new ArrayList<Integer>();
        for (String str : idsArr) list.add(Integer.valueOf(str));
        Map<String, Object> maps = new HashMap<String, Object>();
        if (ordersService.confirmOrders(list)) {
            maps.put(ConstUtils.SUCCESS, true);
            maps.put(ConstUtils.MESSAGE, "订单确认成功！");
        } else {
            maps.put(ConstUtils.SUCCESS, false);
            maps.put(ConstUtils.MESSAGE, "订单确认失败，请重试！");
        }
        return maps;
    }
}
